<template>
  <div class='regist_view'>
    <van-form @submit="onSubmit">
      <van-field
        v-model="username"
        name="username"
        label="用户名"
        placeholder="用户名"
        :rules="[{ required: true, message: '请填写用户名' }]"
      />
      <van-field
        v-model="password"
        type="password"
        name="password"
        label="密码"
        placeholder="密码"
        :rules="[{ required: true, message: '请填写密码' }]"
      />
      <van-field
        v-model="password2"
        type="password"
        name="password2"
        label="重复密码"
        placeholder="密码"
        :rules="[{ required: true, message: '请填写密码' }]"
      />
      <div style="margin: 16px;">
        <van-button round block type="info" native-type="submit" color="linear-gradient(to right, #7965E6, #7484DC)">立即注册</van-button>
      </div>
    </van-form>
  </div>
</template>
<script>

export default {
  data () {
    return {
      username: '',
      password: '',
      password2:''
    }
  },
  methods: {
    onSubmit(values) {
      let _this = this
      if(values.password !== values.password2) {
        _this.$notify({
          type: 'danger',
          message: '两次输入的密码不相同，请检查',
          duration: 1600
        });
      } else {
        let name = values.username
        let obj = {
          name: values.username,
          passd: values.password2
        }
        _this.$api.sumrue.regist_api(obj).then(async res=> {
          console.log(res)
          if(res.code == 0) {
             _this.$toast.fail(res.msg)
          } else if(res.code == 1){
            await window.localStorage.setItem('token',res.data.token)
            _this.$toast.success(res.msg)
            setTimeout(()=> {
              _this.$router.push('/')
            },2000)
          }else {
             _this.$toast.success('啥情况')
          }
        })
      }
    },
  }
}
</script>

<style scoped>
</style>
